using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CostMoney : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_CostMoney
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExImage icon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textValue { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("icon", out var __tbv0);
                this.icon = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textValue", out var __tbv1);
                this.textValue = __tbv1;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private int itemCid = -1;
        private int numCost = -1;
        private bool isChangeCostColor = true;

        protected UIB_UIN_CostMoney ui { get; set; }
        private UIMsgDispatcher currMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        private bool isEnough
        {
            get
            {
                int numHave = ItemModule.Instance.GetNum(this.itemCid);
                return numHave >= this.numCost;
            }
        }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CostMoney();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            this.ClearData();
        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        protected override void AddMsgListeners()
        {
            currMsgDispatcher.AddListener<int>(MsgEventType.OneItemChanged, this.OnMsgOneItemChanged);
        }

        void OnMsgOneItemChanged(int cid)
        {
            if (cid == this.itemCid)
            {
                this.ViewNum(this.numCost, this.isChangeCostColor);
            }
        }

        public void Show(int itemCid, int numCost = 1, bool isChangeCostColor = true)
        {
            CfgItem cfgItem = TableCenter.item.Get(itemCid);
            if (cfgItem == null)
            {
                this.Hide();
                return;
            }
            this.SetData(itemCid, numCost, isChangeCostColor);
            this.Show();

            this.ViewNum(numCost, isChangeCostColor);
            this.SetImageSprite(this.ui.icon, cfgItem.Icon_fullPath);
        }

        private void SetData(int itemCid, int numCost, bool isChangeCostColor)
        {
            this.itemCid = itemCid;
            this.numCost = numCost;
            this.isChangeCostColor = isChangeCostColor;
        }

        private void ClearData()
        {
            this.itemCid = -1;
            this.numCost = -1;
            this.isChangeCostColor = true;
        }

        private void ViewNum(int numCost, bool isChangeCostColor)
        {
            this.ui.textValue.text = numCost.ToString();
            int index = (!isChangeCostColor || this.isEnough) ? 1 : 0;
            //this.ui.textValue.indexSwitchColor = index;
        }

        private bool IsEnough()
        {
            return !this.isActive || this.isEnough;
        }

        public bool CheckIsEnough(bool isTip = true)
        {
            bool isEnough = this.IsEnough();
            if (isTip && !isEnough)
            {
                CfgItem cfgItem = TableCenter.item.Get(this.itemCid);
                NoticeModule.ShowNoticeNoCall(APIUIConst.NoticeCid_ItemNotEnough, cfgItem.Name);
            }
            return isEnough;
        }
    }
}
